/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cms.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.cms.entity.BusinessRepairs;
import com.thinkgem.jeesite.modules.cms.service.BusinessRepairsService;

/**
 * 报修表生成Controller
 * @author 开发者
 * @version 2016-06-18
 */
@Controller
@RequestMapping(value = "${adminPath}/cms/businessRepairs")
public class BusinessRepairsController extends BaseController {

	@Autowired
	private BusinessRepairsService businessRepairsService;

    @Autowired
    private OfficeService officeService;
	
	@ModelAttribute
	public BusinessRepairs get(@RequestParam(required=false) String id) {
		BusinessRepairs entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = businessRepairsService.get(id);
		}
		if (entity == null){
			entity = new BusinessRepairs();
		}
		return entity;
	}

    @RequiresPermissions("sys:office:view")
    @RequestMapping(value = {"index"})
    public String index(Office office, Model model) {
//        model.addAttribute("list", officeService.findAll());
        return "modules/cms/businessRepairsIndex";
    }

    @RequiresPermissions("sys:office:view")
    @RequestMapping(value = "none")
    public String none() {
        return "modules/cms/businessRepairsNone";
    }
	
	@RequiresPermissions("cms:businessRepairs:view")
	@RequestMapping(value = {"list", ""})
	public String list(BusinessRepairs businessRepairs, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BusinessRepairs> page = businessRepairsService.findPage(new Page<BusinessRepairs>(request, response), businessRepairs);
        for(BusinessRepairs businessRepairs1 : page.getList()){
            businessRepairs1.setOffice(officeService.get(businessRepairs1.getOffice().getId()));
        }
		model.addAttribute("page", page);
		return "modules/cms/businessRepairsList";
	}

	@RequiresPermissions("cms:businessRepairs:view")
	@RequestMapping(value = "form")
	public String form(BusinessRepairs businessRepairs, Model model) {
		model.addAttribute("businessRepairs", businessRepairs);
		return "modules/cms/businessRepairsForm";
	}

	@RequiresPermissions("cms:businessRepairs:edit")
	@RequestMapping(value = "save")
	public String save(BusinessRepairs businessRepairs, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, businessRepairs)){
			return form(businessRepairs, model);
		}
		businessRepairsService.save(businessRepairs);
		addMessage(redirectAttributes, "保存报修数据成功");
		return "redirect:"+Global.getAdminPath()+"/cms/businessRepairs/?repage";
	}
	
	@RequiresPermissions("cms:businessRepairs:edit")
	@RequestMapping(value = "delete")
	public String delete(BusinessRepairs businessRepairs, RedirectAttributes redirectAttributes) {
		businessRepairsService.delete(businessRepairs);
		addMessage(redirectAttributes, "删除报修数据成功");
		return "redirect:"+Global.getAdminPath()+"/cms/businessRepairs/?repage";
	}

}